package net.area80.components
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	import net.area80.components.skin.ABScrollbarSkin;
	
	/**
	 * TFScrollbars 
	 * @author wissarut
	 * 
	 */	
	public class TFScrollbars extends Sprite 
	{
		private var nameField:TextField;
		private var PERPAGE:uint;
		private var cPage:Number;
		private var skin:ABScrollbarSkin;
		
		public function TFScrollbars(skin:ABScrollbarSkin,tf:TextField,linePerpage:uint) 
		{
			visible = false;
			initSkin(skin);
			nameField = tf;
			PERPAGE = linePerpage;
			
			nameField.addEventListener(KeyboardEvent.KEY_UP, onTextChange);
			initScroll();
			checkVisible();
			

			addEventListener(Event.ADDED_TO_STAGE, addToStage);
			addEventListener(Event.REMOVED_FROM_STAGE, clearListener);
		}
		public function alignByConent ():void {
			if(nameField) {
				height = nameField.height;
				x = nameField.x+nameField.width;
				y = nameField.y;
			}
		}
		private function initSkin (skin:ABScrollbarSkin):void {
			this.skin = skin;
			
			addChild(skin.bar);
			if(skin.upBtn) addChild(skin.upBtn);
			if(skin.drag) addChild(skin.drag);
			if(skin.downBtn) addChild(skin.downBtn);
			positionSkin ();
			
			if(skin.bar) skin.bar.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownhandler);
			if(skin.drag) skin.drag.mouseChildren = false;
		//	if(skin.drag) skin.drag.addEventListener(MouseEvent.MOUSE_DOWN, dragBarHandler);
			if(skin.upBtn) skin.upBtn.addEventListener(MouseEvent.CLICK, prevHandler);
			if(skin.downBtn) skin.downBtn.addEventListener(MouseEvent.CLICK, nextHandler);
			
		}
		private function nextHandler (e:MouseEvent=null):void {
			// TODO : Next Back
		}
		private function prevHandler (e:MouseEvent=null):void {	
			// TODO : Next Back
		}
		
		public override function set height (h:Number):void {
			var btnHeight:Number = 0;
			if(skin.upBtn) btnHeight += skin.upBtn.height;
			if(skin.downBtn) btnHeight += skin.downBtn.height;
			skin.bar.height = h-btnHeight;
			positionSkin();
		}
		protected function positionSkin ():void {
			if(skin.upBtn) skin.upBtn.y = 0;
			skin.bar.y = (skin.upBtn) ? skin.upBtn.y+skin.upBtn.height : 0;
			if(skin.drag)  skin.drag.y = top;
			if(skin.downBtn) skin.downBtn.y = bottom;
		}
		private function addToStage (e:Event=null):void {
			stage.addEventListener(MouseEvent.MOUSE_WHEEL,mouseWheelHandler);
			addEventListener(Event.REMOVED_FROM_STAGE, dispose);
		}
		private function dispose(e:Event):void {
			nameField.removeEventListener(KeyboardEvent.KEY_UP, onTextChange);
			stage.removeEventListener(MouseEvent.MOUSE_WHEEL, mouseWheelHandler);
		}
		private function mouseWheelHandler (e:MouseEvent):void {
			cPage = nameField.scrollV;
			updatePos();
			e.stopPropagation();
			
		}
		public function updateText ():void {checkVisible();}
		private function onTextChange (e:Event):void {

			checkVisible();
		}
		private function checkVisible ():void {
			cPage = nameField.scrollV;
			if(nameField.numLines>PERPAGE) {
				visible = true;
			} else {
				visible = false;
			}	
			updatePos();
		}
		
		private function initScroll ():void {
			nameField.scrollV = cPage;
			addEventListener(MouseEvent.MOUSE_DOWN, mouseDownhandler);
			resetScrollBar();
		}
		private function mouseDownhandler (e:MouseEvent):void {
			stage.addEventListener(MouseEvent.MOUSE_MOVE, scrollContent);
			stage.addEventListener(MouseEvent.MOUSE_UP, clearListener);
			scrollContent();
		}
		private function clearListener (e:Event):void {
			stage.removeEventListener(MouseEvent.MOUSE_MOVE, scrollContent);
			stage.removeEventListener(MouseEvent.MOUSE_UP, clearListener);
		}
		private  function resetScrollBar ():void {
			skin.drag.y = 0;
		}
		private function scrollContent (e:Event =null):void {
			if(mouseY > top && mouseY< top+skin.bar.height) {
					cPage = 1+Math.round(((mouseY-top)/skin.bar.height) * (nameField.numLines-PERPAGE+1));
					updatePos();
			}
			
		}
		private function updatePos ():void {
				nameField.scrollV = cPage;
				skin.drag.y = top+((cPage-1)/((nameField.numLines)-PERPAGE+1))*(skin.bar.height-skin.drag.height);
		}
		
		private function get top ():Number {			return skin.bar.y;}
		private function get bottom ():Number {			return skin.bar.height+skin.bar.y;}
		private function get dragHeight ():Number {		return (skin.drag) ? skin.drag.height : 0;}
	

	}
}